package com.app.freshmenapp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.app.freshmenapp.entity.User;
import com.app.freshmenapp.service.UserService;

@RestController
@RequestMapping("/user")
public class UserController {
	@Autowired
	UserService userService;
	
	@RequestMapping(value = "/list")
	public Map<String,Object> findAllUser() {
		List<User> userList = userService.findAllUser();
		HashMap<String, Object> map = new HashMap<>();
		map.put("success", true);
		map.put("msg", "成功");
		map.put("list", userList);
		return map;
	}
	@PostMapping("/findone/{id}")
	public Map<String, Object> findUserRestful(@PathVariable("id") Integer id) {
		HashMap<String, Object> map = new HashMap<String, Object> ();
		map.put("success",false);
		map.put("msg", "失败");
		User user = userService.findUser(id);
		if(user!=null) {
			map.put("success",true);
			map.put("msg", "成功");
			map.put("data", user);
		}
		return map;
	}
	@PostMapping("/deleteone/{id}")
	public Map<String,Object> deleteUserRestful(@PathVariable("id") Integer id) {
		HashMap<String, Object> map = new HashMap<String, Object> ();
		map.put("success",false);
		map.put("msg", "失败");
		boolean result = userService.deleteUser(id);
		if(result==true) {
			userService.deleteUser(id);
			map.put("success",true);
			map.put("msg", "成功");
		}
		return map;
	}
	@PostMapping("/regist")
	public Map<String,Object> addUserRestful(@RequestBody User user) {
		HashMap<String, Object> map = new HashMap<String, Object> ();
		boolean result  = userService.addUser(user);
		if(result == true) {
			map.put("success",true);
			map.put("msg", "成功");
		}else {
			map.put("success",false);
			map.put("msg", "失败");
		}
		return map;
	}
	// == @RequestMapping(value="/updateone",method=RequestMethod.PUT)
	@PutMapping("/updateone")
	public Map<String,Object> updateUserRestful(User user) {
		HashMap<String, Object> map = new HashMap<String, Object> ();
		boolean result  = userService.updateUser(user);;
		if(result == true) {
			map.put("success",true);
			map.put("msg", "成功");
		}else {
			map.put("success",false);
			map.put("msg", "失败");
		}
		return map;
		
	}
	@PostMapping("/login")
	public Map<String,Object> login(@RequestBody User user){
		HashMap<String, Object> map = new HashMap<String, Object> ();
		String role = userService.validate(user.getName(),user.getPassword());
		if(role!=null) {
			map.put("success",true);
			map.put("msg", "成功");
			map.put("role", role);
		}else {
			map.put("success",false);
			map.put("msg", "失败");
		}
		return map;
	}
	
	
	


}
